import { userStore } from '@/biz/user'
import server from '@/config/server/index.js'
import dt from '@dt/dt'

let api = {
  create: server.api().post('/api/property/addOrEditProperty'),
  detail: server.api().get('/api/property/selectDetail'),
  focus: server.api().get('/api/customer/concernProperty'),
  list: server.api().post('/api/property/pageList')
}

function create(info) {
  return api.create.fetch(info)
}

function mineList() {
  return api.list.fetch({ createId: userStore().info.id, rows: 130 })
}

function focusList() {
  return api.list.fetch({ tab: 'concern', rows: 130, shelvesStatus: 1 })
}

function detail(id) {
  return api.detail.fetch({ id }).then((res) => {
    res.image = res.pictures.filter((itm) => itm.isCover)[0]?.attachAddress
    if (!res.image) res.image = res.pictures[0]?.attachAddress

    res.propertyTypeStr = dt.dictInfo().__value('propertyType', res.propertyType)
    res.salesUnitStr = dt.dictInfo().__value('salesUnit', res.salesUnit)
    res.decorationDegreeStr = dt.dictInfo().__value('decorationDegree', res.decorationDegree)
    res.statusStr = dt.dictInfo().__value('propertyStatus', res.status)
    res.airConditioningTypeStr = dt.dictInfo().__value('airConditioningType', res.airConditioningType)
    res.transactionWayStr = dt.dictInfo().__value('transactionWay', res.transactionWay)

    res.isGasStr = res.isGas != null ? (res.isGas ? '有' : '无') : null
    res.isCateringStr = res.isCatering != null ? (res.isCatering ? '是' : '否') : null
    res.isNearStreetStr = res.isNearStreet != null ? (res.isNearStreet ? '是' : '否') : null
    res.isRegisterStr = res.isRegister != null ? (res.isRegister ? '是' : '否') : null

    res.showTotalPrice = res.totalPrice + '万元'
    res.showTotalArea = res.totalArea + '万㎡'
    return res
  })
}

function focus(info) {
  return api.focus.fetch({ propertyId: info.propertyId, isCancel: !!info.isConcern })
}

export default {
  create,
  detail,
  focus,
  mineList,
  focusList
}
